/******************************************************************************
 * Product: Adempiere ERP & CRM Smart Business Solution                       *
 * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved.                *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. This program is distributed in the hope   *
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
 * See the GNU General Public License for more details.                       *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 * For the text or an alternative of this public license, you may reach us    *
 * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA        *
 * or via info@compiere.org or http://www.compiere.org/license.html           *
 *****************************************************************************/
package org.compiere.apps.form;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalTheme;

import org.adempiere.plaf.AdempiereLookAndFeel;
import org.adempiere.plaf.AdempiereThemeInnova;
import org.compiere.apps.ConfirmPanel;
import org.compiere.model.X_AD_Form;
import org.compiere.plaf.CompiereColor;
import org.compiere.swing.CPanel;
import org.compiere.util.CLogger;
import org.compiere.util.Env;
import org.compiere.util.ValueNamePair;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;

import com.jgoodies.looks.plastic.PlasticTheme;

//import chrriis.common.UIUtils;
//import chrriis.dj.nativeswing.swtimpl.NativeInterface;
//import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;

/**
 *  Create Charge from Accounts
 *
 *  @author Jorg Janke
 *  @version $Id: VCharge.java,v 1.3 2006/07/30 00:51:28 jjanke Exp $
 */
public class PentahoBrowser implements FormPanel, ActionListener
{
	private CPanel panel = new CPanel();
	
	private X_AD_Form m_adform;

	/**	Window No			*/
	public int         m_WindowNo = 0;
//	/**	FormFrame			*/
//	private FormFrame 	m_frame;

	/** Account Element     */
	public int         m_C_Element_ID = 0;
	/**	Logger			*/
	public static CLogger log = CLogger.getCLogger(Charge.class);	
	/**
	 *	Initialize Panel
	 *  @param WindowNo window
	 *  @param frame parent frame
	 */
	public void init (int WindowNo, FormFrame frame)
	{
		
		log.info("");
		m_WindowNo = WindowNo;
		m_frame = frame;
		m_adform = new X_AD_Form(Env.getCtx(), frame.getAD_Form_ID(), null);
		try
		{
			jbInit();
			dynInit();
		}
		catch(Exception e)
		{
			log.log(Level.SEVERE, "", e);
		}
	}	//	init

	/**	FormFrame			*/
	private FormFrame 	m_frame;
	//

	/**
	 *  Static Init
	 *  @throws Exception
	 */
	private void jbInit() throws Exception
	{
	    
		CompiereColor.setBackground(panel);

	    NativeInterface.open();
	    UIUtils.setPreferredLookAndFeel();
	    SwingUtilities.invokeLater(new Runnable() {
	      public void run() {
//	        JFrame frame = new JFrame("Аналитическая отчетность");
//	        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
	        try {
				m_frame.getContentPane().add(createContent(), BorderLayout.CENTER);
			} catch (ClassNotFoundException | InstantiationException
					| IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        m_frame.setMaximize(true);
	        
	        //m_frame.setSize(800, 600);
	        //m_frame.setLocationByPlatform(true);
//	        m_frame.setVisible(true);
			//	  Look & Feel
			Class<?> lafClass = null;
			try {
				lafClass = Class.forName("org.adempiere.plaf.AdempiereLookAndFeel");
			}
			catch (Exception e)
			{
				log.severe(e.getMessage());
				return;
			}
			s_vp_adempiereTheme = new ValueNamePair("org.adempiere.plaf.AdempiereTheme", AdempiereThemeInnova.NAME);
			ValueNamePair theme = s_vp_adempiereTheme;
			Class<?> c = null;
			try {
				c = Class.forName(theme.getValue());
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			}

			MetalTheme t = null;
			try {
				t = (MetalTheme)c.newInstance();
			} catch (InstantiationException | IllegalAccessException e1) {
				e1.printStackTrace();
			}
		    AdempiereLookAndFeel.setCurrentTheme((PlasticTheme)t);
			try
			{
				UIManager.setLookAndFeel((LookAndFeel)lafClass.newInstance());
			}
			catch (Exception e)
			{
				log.severe(e.getMessage());
			}
	      }
	    });
	    //NativeInterface.runEventPump();
		
		//
		//mainPanel.add(newPanel, BorderLayout.NORTH);
		//mainPanel.add(accountPanel, BorderLayout.CENTER);
		//accountPanel.add(accountOKPanel, BorderLayout.SOUTH);
		//accountPanel.add(dataPane, BorderLayout.CENTER);
	}   //  jbInit

	/**
	 * 	Dispose
	 */
	public void dispose()
	{
		if (m_frame != null)
			m_frame.dispose();
		m_frame = null;
	}	//	dispose

	/**
	 *	Action Listener
	 *  @param e event
	 */
	public void actionPerformed (ActionEvent e)
	{
		log.info(e.getActionCommand());
		//

		if (e.getActionCommand().equals(ConfirmPanel.A_OK) || m_C_Element_ID == 0)
			dispose();
		//  new Account
	}   //  actionPerformed


	
	/**
	 *  Dynamic Init
	 *  - Get defaults for primary AcctSchema
	 *  - Create Table with Accounts
	 */
	private void dynInit()
	{
	}

	private static ValueNamePair    s_vp_adempiereTheme = null;
	
	public  JComponent createContent() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
	    JPanel contentPane = new JPanel(new BorderLayout());
	    //JPanel webBrowserPanel = new JPanel(new BorderLayout());
	    //webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));
		    final JWebBrowser webBrowser = new JWebBrowser();
//		    PentahoBrowser.getJSPURL();
		    webBrowser.navigate(m_adform.getJSPURL());
		    //webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
		    //contentPane.add(webBrowserPanel, BorderLayout.CENTER);
		    contentPane.add(webBrowser, BorderLayout.CENTER);
		    webBrowser.setMenuBarVisible(false);
		    webBrowser.setLocationBarVisible(false);
		    webBrowser.setBarsVisible(false);
		    
		    // Create an additional bar allowing to show/hide the menu bar of the web browser.
		    //JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
		    /*JCheckBox menuBarCheckBox = new JCheckBox("Menu Bar", webBrowser.isMenuBarVisible());
		    menuBarCheckBox.addItemListener(new ItemListener() {
		      public void itemStateChanged(ItemEvent e) {
		        webBrowser.setMenuBarVisible(e.getStateChange() == ItemEvent.SELECTED);
		      }
		    });*/
		    //buttonPanel.add(menuBarCheckBox);
		    //contentPane.add(buttonPanel, BorderLayout.SOUTH);
		    return contentPane;
		  }
}   //  
